// space-buccaneer
//
// engine.cpp


// includes
#include <SFML/Graphics.hpp>
#include "engine.h"
#include "gamestate.h"
#include "iostream"

void CGameEngine::Init(const char* title, int width, int height, int bpp, bool fullscreen)
{
    sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
    App = new sf::RenderWindow (sf::VideoMode(width, height, desktop.bitsPerPixel), title);

    //center the window on desktop
    App->setPosition(sf::Vector2i((sf::VideoMode::getDesktopMode().width/2)-width/2, (sf::VideoMode::getDesktopMode().height/2)-height/2));

    //App->setVerticalSyncEnabled(true);

    m_running = true;
}

void CGameEngine::Cleanup()
{
    // cleanup the all states
    while ( !states.empty() )
    {
        states.back()->Cleanup();
        states.pop_back();
    }

    // shutdown SFML
    App->close ();
}

void CGameEngine::ChangeState(CGameState* state)
{
    // cleanup the current state
    if ( !states.empty() )
    {
        states.back()->Cleanup();
        states.pop_back();
    }

    // store and init the new state
    states.push_back(state);
    states.back()->Init();
}

void CGameEngine::PushState(CGameState* state)
{
    // pause current state
    if ( !states.empty() )
    {
        states.back()->Pause();
    }

    // store and init the new state
    states.push_back(state);
    states.back()->Init();
}

void CGameEngine::PopState()
{
    // cleanup the current state
    if ( !states.empty() )
    {
        states.back()->Cleanup();
        states.pop_back();
    }

    // resume previous state
    if ( !states.empty() )
    {
        states.back()->Resume();
    }
}

void CGameEngine::HandleEvents()
{
    // let the state handle events
    states.back()->HandleEvents(this);
}

void CGameEngine::Update()
{
    // let the state update the game
    states.back()->Update(this);
}

void CGameEngine::Draw()
{
    // let the state draw the screen
    states.back()->Draw(this);
}
